<?php

namespace addons\customform\controller;

use app\common\exception\UploadException;
use app\common\library\Email;
use app\common\library\Upload;
use think\addons\Controller;
use think\File;

class ApiClient extends Controller
{
    public function _initialize()
    {
        parent::_initialize();

    }
    //获取单条配置数据
    public function design_data()
    {

        $id = input("id", 0, "intval");

        $result = db("customform")->where(["id" => $id, 'status' => '1'])->cache(true)->find();

        if (!$result) {
            return json(['code' => 0, 'msg' => '数据不存在']);
        }

        if ($result['is_login_switch'] && empty((int) $this->auth->id)) {
            return json(['code' => 401, 'msg' => '请登陆']);
        }

        $result['config'] = json_decode($result['config'], true);

        return json(['code' => 1, "data" => $result]);
    }

    //用户提交表单数据
    public function submit_data()
    {
        $id    = input("id", 0, "intval");
        $data  = input("data/a", [], 'htmlspecialchars');
        $token = input("__token__", '');

        $validate = $this->validate(
            [
                'id'        => $id,
                '__token__' => $token,
            ],
            [
                'id'        => 'require',
                '__token__' => 'require|token',
            ]);
        if (true !== $validate) {
            // 验证失败 输出错误信息
            return json(['code' => 0, 'msg' => $validate, '__token__' => $this->request->token()]);
        }

        $result = db("customform")->where(["id" => $id, 'status' => '1'])->find();

        if (!$result) {

            return json(['code' => 0, 'msg' => '数据不存在', '__token__' => $this->request->token()]);
        }

        if ($result['is_login_switch'] && empty((int) $this->auth->id)) {
            return json(['code' => 401, 'msg' => '请登陆', '__token__' => $this->request->token()]);
        }
        
        $result['config'] = json_decode($result['config'], true);

        $newdata = [];

        foreach ($data as $key => $value) {
            $newdata[] = ["id" => $key, "value" => $value];
        }

        if (count($newdata) == 0) {
            return json(['code' => 0, 'msg' => '数据不能为空', '__token__' => $this->request->token()]);
        }

        //需后台记录的字段
        $showfield = $result['config']['formconfig']['showfield'];
        //显示字段
        $showfieldlist = $result['config']['formconfig']['showfieldlist'];

        //查询需后台记录的字段是否存在，不存在则默认前一行作为标题
        if (count($showfield) > 0) {

            foreach ($showfield as $key => $value) {
                foreach ($newdata as $keys => $values) {
                    if ($value == $values['id']) {
                        //递归出标题
                        $name         = $this->search_title($value, $result['config']['dataconfig']);
                        $title[$name] = $values['value'];
                        continue;
                    }
                }
            }

            if (empty($title)) {
                foreach ($showfieldlist as $key => $value) {
                    if ($value['id'] == $showfield[0]) {
                        return json(['code' => 0, 'msg' => $value['name'] . '字段不能为空', 'data' => $showfield[0], '__token__' => $this->request->token()]);
                    }
                }
            }

        } else {
            $name         = $this->search_title($newdata[0]['id'], $result['config']['dataconfig']);
            $title[$name] = $newdata[0]['value'];
        }

        //查询必须填写的值
        $nempty_list = $this->nempty_value($result['config']['dataconfig']);
        //联动事件规则
        $linkageEvent = $result['config']['linkageEvent'];
        //过滤不检验的数组
        $linkageEventArray = [];
        
        //过滤出符合规则并且隐藏的字段
        foreach ($linkageEvent as $key => $value) {
            $event_status = true;
            foreach ($value['from'] as $keys => $values) {
                if( !isset($data[$values['id']]) || $data[$values['id']] != $values['value']){
                    $event_status = false;
                }
            }
            if($event_status){
                //过滤数组增加元素
                foreach ($value['obj'] as $keys => $values) {
                    $linkageEventArray[] = $values['id'];
                }
            }
        }
        //递归过滤数据，判断是否为容器，若为容器则增加容器里的字段
        foreach ($linkageEventArray as $key => $value) {
            if(!isset($data[$value])){
                $box_id = $value;
                unset($linkageEventArray[$key]);
                $linkageEventArray = array_merge($linkageEventArray,$this->get_box_list($result['config']['dataconfig'],$box_id));
            }
        }

        foreach ($nempty_list as $key => $value) {
            //检验数据是否存在
            if (!$this->search_value($newdata, $value)) {
                if(array_search((string)$value['id'],$linkageEventArray) === false){
                    return json(['code' => 0, 'msg' => $value['config']['label'] . $value['config']['message'], 'data' => $value['id'], '__token__' => $this->request->token()]);
                }
            }
        }

        //如果存在过滤字段已输入内容，则清除
        foreach ($newdata as $key => $value) {
            if(array_search((string)$value['id'],$linkageEventArray) !== false){
                unset($newdata[$key]);
            }
        }

        $insert['user_id']       = empty((int) $this->auth->id) ? 0 : $this->auth->id;
        $insert['customform_id'] = $id;
        $insert['data']          = json_encode($newdata);
        $insert['weigh']         = (db("customform_data")->max('id') + 1);
        $insert['createtime']    = time();
        $insert['updatetime']    = time();
        $insert['title']         = json_encode($title, JSON_UNESCAPED_UNICODE);

        $rs = db("customform_data")->insert($insert);

        if ($rs) {

            //邮箱通知
            if ($result['is_email_switch']) {

                $email = new Email;
                $email->to($result['email'])->subject("表单通知")->message("您的表单：" . $result['title'] . "，收到了一条数据信息，请注意查收")->send();
            }
            return json(['code' => 1, 'msg' => '提交成功', '__token__' => $this->request->token()]);
        } else {
            return json(['code' => 0, 'msg' => '提交失败，请重试', '__token__' => $this->request->token()]);
        }
    }

    //根据id搜索字段
    protected function get_box_list($array,$id)
    {
        foreach ($array as $key => $value) {
            if($value['id'] == $id && !empty($value['list'])){
                return $this->get_box_all($value['list']);
            }
        }
        return false;
    }

    //递归返回过滤字段
    protected function get_box_all($array)
    {
        $new_array = [];
        foreach ($array as $key => $value) {
            if (empty($value['list'])) {
                $new_array[] = $value['id'];
            } else {
                $new_array = array_merge($new_array, $this->get_box_all($value['list']));
            }
        }
        return $new_array;
    }

    //检验提交值
    protected function search_value($array, $data)
    {
        $check = false;

        foreach ($array as $key => $value) {
            if ($value['id'] == $data['id']) {
                //正则不为空
                if (($data['config']['regular'] && !preg_match($data['config']['regular'], $value['value'])) || empty($value['value'])) {

                    return false;
                } else {
                    $check = true;
                    continue;
                }
            }
        }

        return $check;
    }

    //查询总数据中不能为空的值
    protected function nempty_value($array)
    {
        $new_array = [];
        foreach ($array as $key => $value) {
            if (empty($value['list'])) {
                if (!empty($value['config']['required'])) {
                    $new_array[] = $value;
                }
            } else {
                $new_array = array_merge($new_array, $this->nempty_value($value['list']));
            }
        }

        return $new_array;
    }

    //查询lable
    protected function search_title($id, $array)
    {
        foreach ($array as $key => $value) {
            if (empty($value['list'])) {
                if ($value['id'] == $id) {
                    return $value['config']['label'];
                }
            } else {
                if ($result = $this->search_title($id, $value['list'])) {
                    return $result;
                }
            }
        }
    }

    //保存base64图片
    public function save_sign_img()
    {
        $imgdata = $this->request->post('imgdata', '');

        if (!$imgdata) {
            return json(['code' => 0, 'msg' => '图片数据错误']);
        }

        $reg = '/data:image\/(\w+?);base64,(.+)$/si';
        preg_match($reg, $imgdata, $match_result);

        if (!in_array($match_result[1], ['png', 'jpg'])) {
            return json(['code' => 0, 'msg' => '图片数据错误']);
        }

        $file_name = uniqid() . '.' . $match_result[1];

        $wwwpath = DS . 'uploads' . DS . date('Ymd');
        $path    = ROOT_PATH . 'runtime' . DS . 'addons' . DS . 'customform' . $wwwpath;

        if (!is_dir($path)) {
            @mkdir($path, 0755, true);
        }

        file_put_contents($path . DS . $file_name, base64_decode($match_result[2]));

        try {
            $file_info = getimagesize($path . DS . $file_name);

            if (!$file_info || !isset($file_info[0]) || !isset($file_info[1])) {
                throw new UploadException('当前文件不是图片');
            }

            $file_more = [
                "type"     => $file_info['mime'],
                "suffix"   => $match_result[1],
                "name"     => $file_name,
                "size"     => filesize($path . DS . $file_name),
                "tmp_name" => $path . DS . $file_name,
            ];

            $file = (new File($path . DS . $file_name))->setUploadInfo($file_more);
            $file->isTest(true);
            $upload     = new Upload($file);
            $attachment = $upload->upload();
        } catch (UploadException $e) {
            unlink($path . DS . $file_name);
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }

        return json(['code' => 1, 'data' => $attachment->url]);
    }
}
